package YZ.Students.Mier.controller.web;

import YZ.Students.Mier.Service.ICommentService;
import YZ.Students.Mier.Service.impl.CommentServiceImpl;
import YZ.Students.Mier.Tool.utils.BaseConstants;
import YZ.Students.Mier.controller.BaseServlet;
import YZ.Students.Mier.entity.CommentEntity;
import YZ.Students.Mier.entity.UserEntity;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.annotation.WebServlet;
import java.util.Map;
@WebServlet("/commentServlet/*")
public class CommentServlet extends BaseServlet {

    private ICommentService commentService = new CommentServiceImpl();

    public String add() throws Exception {
        UserEntity loginUser = (UserEntity) req.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            return outJson(100, "请先登录");
        }
        // 1.获取表单数据
        Map<String, String[]> parameterMap = req.getParameterMap();
        // 2.准备实体类
        CommentEntity commentEntity = new CommentEntity();
        // 3.把Map中的数据拷贝到实体类中
        BeanUtils.populate(commentEntity, parameterMap);
        commentEntity.setUserId(loginUser.getId());
        commentService.save(commentEntity);
        //重定向
        return BaseConstants.BASE_REDIRECT+":invitataionServlet/info?id="+commentEntity.getEntityId();
    }
}
